Ext.namespace("TR.admin.index");

TR.admin.index.channel = function(panelId, title, iconClz){
	var allChannel = [];
	loadChannelTree();
	var troot = new Ext.tree.TreeNode({
		text : "root",
		cId : 0
	});

	var parentChannelId = null;
	var channelTreePanel = new Ext.tree.TreePanel({
		title : "系统模块树",
		flex : 2,
		frame : true,
		root : troot,
		rootVisible : false,
		autoScroll : true,
		listeners : {
			click : function(node){
				parentChannelId = node.attributes.cId;
				childChannelStore.reload();
			}
		}
	});

	var optBar = [{
		text : "创建",
		iconCls : "create",
		handler : function(){
			addChannel("admin/channel/add");
		}
	}, "-", {
		text : "删除",
		iconCls : "delete",
		handler : function(){
			var rec = sm.getSelected();
			if(rec===undefined){
				showFailMsg("请选择子栏目.");
				return;
			}
			confirmDelete(del);
			function del(){
				ajaxRequest({
					url : "admin/channel/delete/" + rec.get("id"),
					showMsg : "删除成功.",
					callback : function(){
						childChannelStore.reload();
					}
				});
			}
		}
	}, "-", {
		text : "更新",
		iconCls : "update",
		handler : function(){
			updateChannel("admin/channel/update");
		}
	}, "-", {
		text : "切换状态",
		iconCls : "toggleStatus",
		handler : function(){
			var rec = sm.getSelected();
			if(rec===undefined){
				showFailMsg("请选择模块.");
				return;
			}
			ajaxRequest({
				url : "admin/channel/toggleStatus/" + rec.get("id"),
				showMsg : "切换状态成功.",
				callback : function(){
					childChannelStore.reload();
				}
			});
		}
	}];

	var childChannelStore = new Ext.data.JsonStore({
		url : "admin/channel/listChannelByParent",
		totalProperty : "total",
		root : "rows",
		fields : ["id", "name", "type", "customLink", "customLinkUrl",
			"isIndex", "isTopNav", "recommend", "status", "orderNum",
			"navOrder", "des", "iconClz"],
		listeners : {
			beforeload : function(store){
				store.removeAll();
				Ext.apply(store.baseParams, {
					id : parentChannelId
				});
			}
		}
	});

	var cm = new Ext.grid.ColumnModel([{
		header : "栏目名称",
		dataIndex : "name"
	}, {
		header : "栏目类型",
		dataIndex : "type"
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderStatus,
		width : 35
	}, {
		header : "图标",
		dataIndex : "iconClz",
		width : 50
	}, {
		header : "顺序",
		dataIndex : "orderNum",
		width : 35
	}, {
		header : "导航顺序",
		dataIndex : "navOrder",
		width : 60
	}, {
		header : "是否自定义链接",
		dataIndex : "customLink",
		width : 90,
		renderer : renderYesOrNo
	}, {
		header : "自定义链接地址",
		dataIndex : "customLinkUrl"
	}, {
		header : "是否首页显示内容",
		dataIndex : "isIndex",
		renderer : renderYesOrNo
	}, {
		header : "是否导航",
		dataIndex : "isTopNav",
		width : 60,
		renderer : renderYesOrNo
	}, {
		header : "描述",
		dataIndex:"des"
	}]);
	var sm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});

	var childGrid = new Ext.grid.GridPanel({
		title : "子栏目列表",
		frame : true,
		flex : 8,
		store : childChannelStore,
		cm : cm,
		sm : sm,
		tbar : genToolBar(optBar, [], panelId)
	});

	return new Ext.Panel({
		title : title,
		id : panelId,
		iconCls : iconClz,
		closable : true,
		layout : "hbox",
		layoutConfig : {
			align : "stretch"
		},
		items : [channelTreePanel, childGrid]
	});

	function loadChannelTree(){
		ajaxRequest({
			url : "admin/channel/allChannel",
			callback : function(json){
				allChannel = $.makeArray(json.data);
				initChannelTree();
			}
		});
	}

	function initChannelTree(){
		var levelOne = $.grep(allChannel, function(o){
			return o.parent==null
		});
		$.each(levelOne, function(n, v){
			var cNode = new Ext.tree.TreeNode({
				cId : v.id,
				text : v.name,
				iconCls : v.iconClz,
				expanded : true
			});
			troot.appendChild(cNode);
			addChildren(v, cNode);

			function addChildren(p, root){
				$.each(allChannel, function(n, m){
					if(m.parent!=null && m.parent.id== p.id){
						var child = new Ext.tree.TreeNode({
							cId : m.id,
							text : m.name,
							iconCls : m.iconClz,
							expanded : true
						});
						root.appendChild(child);
						addChildren(m, child);
					}
				});
			}
		});
		channelTreePanel.expandAll();
//		troot.cascade(function(t){
//			t.addListener("beforeclick", function(){
//				if(!t.hasChildNodes()){
//					return false;
//				}
//			});
//		});
	}

	function genAddOrUpdateWindow(url){
		return new Ext.Window({
			title : "新增或修改栏目",
			layout : "fit",
			modal : true,
			iconCls : "save",
			width : 420,
			height : 480,
			plain : true,
//			bodyStyle : 'padding:5px;',
			items : genForm(url)
		});
		function genForm(url){
			return new Ext.form.FormPanel({
				url : url,
				labelWidth : 100,
				labelAlign : "top",
				bodyStyle:'padding:5px 5px 0',
				frame : true,
				items : [{
					layout : "column",
					items : [{
						columnWidth : 0.5,
						layout : "form",
						defaults : {
							xtype : "textfield",
							height : 22,
							anchor : "95%"
						},
						items : [{
							xtype : "textfield",
							fieldLabel : "栏目名称",
							allowBlank : false,
							name : "name"
						}, {
							xtype : "textfield",
							fieldLabel:"父栏目",
							readOnly : true,
							value : channelTreePanel.getSelectionModel().getSelectedNode().text
						}, {
							xtype : "hidden",
							name : "pId",
							value : channelTreePanel.getSelectionModel().getSelectedNode().attributes.cId
						}, genStatusCombo(), {
							xtype : "numberfield",
							fieldLabel : "顺序",
							allowBlank : false,
							name : "orderNum"
						}, {
							xtype : "numberfield",
							fieldLabel:"导航顺序",
							name : "navOrder"
						}, {
							xtype : "textfield",
							fieldLabel : "图标",
							name : "iconClz"
						}]
					}, {
						columnWidth : 0.5,
						layout : "form",
						defaults : {
							xtype : "textfield",
							height : 22,
							anchor : "95%"
						},
						items : [genChannelTypeCombo(), genYesOrNotRadioGroup("是否自定义链接", "customLink"), {
								xtype : "textfield",
								fieldLabel : "链接地址",
								name : "customLinkUrl"
							}, genYesOrNotRadioGroup("是否首页显示","isTopNav"),
							genYesOrNotRadioGroup("是否导航菜单","isIndex"),
							genYesOrNotRadioGroup("是否推荐", "recommend")
						]
					}]
				}, {
					fieldLabel : "描述",
					xtype : "textarea",
					anchor : "98%",
					name : "des"
				}, {
					xtype : "hidden",
					name : "id",
					//value设置-1，为空则controller检验失败
					value : "-1"
				}],
				buttonAlign : "center",
				buttons : [{
					text : "保存",
					iconCls : "save",
					handler : function(b){
						submitForm(b.findParentByType("form"), refreshGrid);
						function refreshGrid(){
							childChannelStore.reload();
						}
					}
				}, {
					text : "重置",
					iconCls : "reset",
					type : "reset",
					handler : function(b){
						b.findParentByType("form").getForm().reset();
					}
				}, {
					text : "关闭",
					iconCls : "close",
					handler : function(b){
						b.findParentByType("window").close();
					}
				}]
			});
		}
	}

	function addChannel(url){
		if(channelTreePanel.getSelectionModel().getSelectedNode()==null){
			showFailMsg("请选择父模块");
			return;
		}
		var win = genAddOrUpdateWindow(url);
		win.show();
	}
	function updateChannel(url){
		var rec = sm.getSelected();
		if(rec==undefined){
			showFailMsg("请选择子模块.");
			return;
		}
		var win = genAddOrUpdateWindow(url);
		win.findByType("form")[0].getForm().loadRecord(rec);
		win.show();
	}
}
registerModule("TR.admin.index.channel", TR.admin.index.channel);